<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ include file="/WEB-INF/views/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Insert title here</title>
	<style type="text/css">
	</style>
  <script type="text/javascript">
	var datagrid;
	var rowEditor=undefined;
	var buttonFlag = null;
	var userid=${sessionScope.user.id};
	$(function(){
		datagrid=$("#dg").datagrid({
			url:"${cs}/cs/credit/loanbid/list",//加载的URL
		    isField:"id",
			pagination:true,//显示分页
			pageSize:10,//分页大小
			pageList:[10,15,20],//每页的个数
			fit:true,//自动补全
			fitColumns:true,
			iconCls:"icon-save",//图标
			columns:[[      //每个列具体内容
		              {field:'id', hidden:true },   
		              {field:'title', title:'标名',width:"10%",align:'center' },
		              {field:'content', title:'内容',width:"15%",align:'center' },
		              {field:'publisherName', title:'发布者账户',width:"10%",align:'center' },
		              {field:'securityName', title:'担保账户',width:"10%",align:'center' },
		              {field:'money', title:'借款金额',width:"10%",align:'center' ,
		            	  formatter: function(val,row, index) {
			            		return "￥"+val;
			            	} 
		              },
		              {field:'months', title:'贷款期限',width:"10%",align:'center',
		            	  formatter: function(val,row, index) {
			            		return val+"个月";
			            	}   
		              },
		              {field:'interest', title:'月息百分比',width:"15%",align:'center',
		            	  formatter: function(val,row, index) {
			            		return val*100+"%";
			            	}   
		              },
		              {field:'profit', title:'可获利',width:"10%",align:'center',
		            	  formatter: function(val,row, index) {
			            		return "￥"+(row.money*row.months*row.interest);
			            	}   
		              },
		              {field:'credittime', title:'资金到账时间',  width:"15%", align:'center',
		            	  formatter:function(value,row,rowIndex) { 
		            		  if(row.state>3&& row.state!=9){
		            		  var date = new Date(value);//long转换成date
		                      var month = (date.getMonth() + 1);
		                      var day = date.getDate().toString();
		                      if (month < 10) {
		                          month = "0" + month;
		                      }
		                      if (day < 10) {
		                          day = "0" + day;
		                      }
		                      return date.getFullYear().toString() + "年" + month + "月" + day+"日"+date.getHours()+"时"+date.getMinutes()+"分"+date.getSeconds()+"秒";
		            		 } 
		            	} 
			         },
		             {field:'state', title:'审核状态',  width:"11%", align:'center',
			            	formatter: function(val,row, index) {
			            		switch(row.state){
			            			case 1:return '<span style="color:#FF44AA">待担保人确认</span>';
			            			case 2:return '<span style="color:#FFCC22">待系统审核</span>';
			            			case 3:return '<span style="color:#33FF33">审核通过，已发布</span>';
			            			case 4:return '<span style="color:#0000FF">同意贷款</span>';
			            			case 5:return '<span style="color:#4B0082">逾期垫付</span>';
			            			case 6:return '<span style="color:#000000">已还款</span>';
			            			case 9:return '<span style="color:red">审核未通过</span>';
			            			default: return;
			            		}
			            	}  
			              },
			         {field:'crediterName', title:'放款账户',width:"10%",align:'center' },
			         {field:'reason', title:'不通过理由',width:"20%",align:'center' },
		          ]],
			toolbar:[              //工具条
			        {text:"申请贷款标",iconCls:"icon-add",handler:function(){//回调函数
			        	 buttonFlag="add";
			        	 $('#dd').dialog('open');
			        }},
			        {text:"修改未通过贷款标",iconCls:"icon-edit",handler:function(){
			        	buttonFlag="edit";
			        	var rows=datagrid.datagrid('getSelections');
			        	if(rows.length==1)
			        	{
			        		if(rows[0].publisher==userid && rows[0].state==9){
			        			$('#dd').dialog('open');
			        			$('#id').val(rows[0].id);
			        			$("#title").textbox("setValue",rows[0].title);
			        			$("#content").textbox("setValue",rows[0].content);
			        			$("#money").numberbox("setValue",rows[0].money);
			        			$("#interest").numberbox("setValue",rows[0].interest);
			        			$("#months").numberbox("setValue",rows[0].months);
			        			$('#security').combobox({
				   					 url:'${cs}/cs/credit/loanbid/loanCombobox?money='+rows[0].money,
				   					 valueField:'id',
				   					 textField:'username',
				   					 onLoadSuccess:function(data){
				   						 if(data!=null && data.length>0){
					   						 for(var i=0;i<data.length;i++){
					   							 if(data[i].id==rows[0].security){
								   						$('#security').combobox("setValue",rows[0].security);
								   						break;
					   							 }
					   						 }
				   						 }else{
				   							$.messager.alert('错误', "您的贷款金额过大，暂无可担保人员！", 'error');
				   						 }
				   					 }
				   				 });
			        		}else{
			        			$.messager.alert('错误', "您只能操作自己审核未通过的贷款标！", 'error');
			        		}
			        	} else {
			        		$.messager.alert('错误', "只能选择一个数据进行修改", 'error');
			        	}
			        }},
			        {text:"同意担保",iconCls:"icon-remove",handler:function(){
			        	var rows=datagrid.datagrid('getSelections');
			        	if(rows.length==1){
			        		if(rows[0].security==userid && rows[0].state==1){
			        			$.post("${cs}/cs/credit/loanbid/agreeSecurity?id="+rows[0].id,function(r){
			        				if(r.state==1)datagrid.datagrid('reload');
			        				$.messager.show({
			        					title:r.state==1?"Success":"Error",
			        					msg:r.msg
			        				})
			        			},"json")
			        		}else{
			        			$.messager.alert('错误', "您只能操作需要您担保的贷款标！", 'error');
			        		}
			        	} else {
			        		$.messager.alert('错误', "只能选择一个数据进行修改", 'error');
			        	}
			        }},
			        {text:"投标",iconCls:"icon-filter",handler:function(){
			        	var rows=datagrid.datagrid('getSelections');
			        	if(rows.length==1){
			        		if(rows[0].state==3){
			        			$.post("${cs}/cs/credit/loanbid/agreeCredit?id="+rows[0].id,function(r){
			        				if(r.state==1)datagrid.datagrid('reload');
			        				$.messager.show({
			        					title:r.state==1?"Success":"Error",
			        					msg:r.msg
			        				})
			        			},"json")
			        		}else{
			        			$.messager.alert('错误', "您只能操作需要您担保的贷款标！", 'error');
			        		}
			        	} else {
			        		$.messager.alert('错误', "只能选择一个数据进行修改", 'error');
			        	}
			        }},
			        {text:"还款",iconCls:"icon-redo",handler:function(){
			        	var rows=datagrid.datagrid('getSelections');
			        	if(rows.length==1){
			        		if(rows[0].state==4 && rows[0].publisher==userid){
			        			$.post("${cs}/cs/credit/loanbid/repayment?id="+rows[0].id,function(r){
			        				if(r.state==1)datagrid.datagrid('reload');
			        				$.messager.show({
			        					title:r.state==1?"Success":"Error",
			        					msg:r.msg
			        				})
			        			},"json")
			        		}else{
			        			$.messager.alert('错误', "您只能操作自己的贷款标进行还款！", 'error');
			        		}
			        	} else {
			        		$.messager.alert('错误', "只能选择一个数据进行修改", 'error');
			        	}
			        }}
			  ],
				onLoadSuccess:function(data){
				   	  $('.function').linkbutton({
				   	      iconCls: 'icon-edit'
				   	  });
			   	  }
		});
		 $('#dd').dialog({
             title: buttonFlag=="add"?"修改贷款标":"申请贷款标",
             closed:true,
             modal: true, //dialog继承自window，而window里面有modal属性，所以dialog也可以使用
             toolbar: [{
					text:'Ok',
				iconCls:'icon-ok',
				handler:function(){
					$('#ff').form('submit',{
						onSubmit:function(){
							var isValid = $(this).form('validate');
							if(isValid) {
							}
							return isValid;
						},
						success: function(r){
							if (r>0) {
    							$.messager.show({
    								msg : '操作成功',
    								title : 'Success!'
    							});
    							$('#ff').form('clear');
    							$('#dd').dialog('close');
    							datagrid.datagrid('reload');
    						} else {
    							$.messager.alert('错误', '操作失败', 'error');
    						}
						}
					});
				
				}
			},{
				text:'Cancel',
				handler:function(){
					$('#dd').dialog('close');
					$('#ff').form('clear');
				}
			}],
			onClose: function() {
				$('#ff').form('clear');
				datagrid.datagrid('unselectAll');
				datagrid.datagrid('reload');
			}
         });
		$("#btn_search").click(function(){
			var param = $("#searchForm").serializeJson();
			datagrid.datagrid('load', param);
		});
		$('#state').combobox({
			 data: [{"name": '全部', "id": '-1'},
			        {"name": '未通过', "id": '9'},
			        {"name": '待担保人确认', "id": '1'},
			        {"name": '待系统审核', "id": '2'},
			        {"name": '审核通过，已发布', "id": '3'},
			        {"name": '同意贷款', "id": '4'},
			        {"name": '逾期垫付', "id": '5'},
			        {"name": '已还款', "id": '6'}],
			 valueField:'id',
			 textField:'name',
			 value:'-1'
		 });
		$("#security").next("span").click(function(){
			var money=$("#money").textbox('getValue');
			if(money==null || ''==money){
				$.messager.alert('错误',"请先输入贷款金额，我们将根据您的贷款金额确定担保人名单！", 'error');
			}else{
				$('#security').combobox({
					 url:'${cs}/cs/credit/loanbid/loanCombobox?money='+money,
					 valueField:'id',
					 textField:'username'
				 });
			}
		})
		$("input",$("#money").next("span")).blur(function(){
			if($("#money").val()!=''){
				$('#security').combobox("unselect",$('#security').combobox('getValues'));
			}
		 })
	});
    </script>
</head>
<body style="padding:0 4px; margin:0;  overflow: hidden; ">
<div class="easyui-layout" style="width:100%;height:100%;" data-options="fit:true">
		<div title="信贷管理" data-options="region:'north'" style="height:70px">
		 <form id="searchForm" >
				<table cellpadding="5">
		    		<tr>
		    			<td>贷款标名:</td>
		    			<td><input class="easyui-textbox" type="text"   name="title" ></input></td>
		    			<td>内容关键字:</td>
		    			<td><input class="easyui-textbox" type="text"   name="content" ></input></td>
		    			<td>审核状态:</td>
		    			<td><input class="easyui-combobox" type="text" data-options="panelHeight:'auto'" id="state"  name="state" ></input></td>
		    			<td ><a href="#" id="btn_search" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:80px">Search</a></td>
		    		</tr>
				</table>
			</form>
		</div>
		<div data-options="region:'center'" >
			<table id="dg" >
			</table>
		</div>
		<div id="dd" title="My Dialog"  style="width:300px;height:380px; text-align: center; " data-options="closed:true"> 
				    <form id="ff" method="post" action="${cs}/cs/credit/loanbid/saveLoanbid" >
				    		<input type="hidden" id="id" name="id">
				    		<div style="margin:10px 0">
								贷款标名:<input class="easyui-textbox" type="text" id="title"  name="title" data-options="required:true"></input>
							</div>
							<div style="margin:10px 0">
								贷款内容:<input class="easyui-textbox" id="content" name="content" data-options="multiline:true,required:true"  style="width:171px;height:60px;">
							</div>
							<div style="margin:10px 0">
								贷款金额:<input class="easyui-numberbox" type="text" id="money" precision="2" name="money" min="0.01" data-options="required:true"></input>
							</div>
							<div style="margin:10px 0">
								月息百分比:<input class="easyui-numberbox" type="text" id="interest" precision="3"  name="interest" min="0.001" data-options="required:true"></input>
							</div>
							<div style="margin:10px 0">
								时长:<input class="easyui-numberbox" type="text" id="months"  name="months" data-options="required:true"></input>个月
							</div>
							<div style="margin:10px 0">
								担保人:<input class="easyui-combobox" type="text" id="security"  name="security" data-options="required:true,panelHeight:'auto'"></input>
							</div>
				    </form>
			</div>
		</div>
</body>
</html>